/*
 * Copyright (C) 2009 - 2010 TrinityCore <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "ScriptPCH.h"
#include "ObjectMgr.h"
#include "ScriptMgr.h"
#include "ScriptedCreature.h"
#include "SpellAuraEffects.h"
#include "icecrown_citadel.h"

enum Texts
{
	SAY_BERSERK						= 1,
	SAY_HORROR						= 2,
	SAY_GHULS						= 3,
	SAY_DEATH						= 4,
	SAY_COMBAT						= 5,
	SAY_KILL						= 6,
};

enum Spels 
{
	SPELL_SUMMONING_HORROR			= 70372,
	SPELL_SUMMONING_GHULS			= 70358,
};

enum Events
{
	EVENT_BERSERK					= 1,
	EVENT_SUMMONING_HORROR			= 2,
	EVENT_SUMMONING_GHULS			= 3,
};


class boss_the_lich_king : public CreatureScript
{
	public:
		boss_the_lich_king() : CreatureScript("boss_the_lich_king") { }
		struct boss_the_lich_kingAI : BossAI
		{
			boss_the_lich_kingAI(Creature* creature) : BossAI(creature,  DATA_THE_LICH_KING)
			{
			}

			void Reset()
			{
				BossAI::Reset();
				me->SetReactState(REACT_DEFENSIVE);
				events.ScheduleEvent(EVENT_BERSERK, 900000);
				events.ScheduleEvent(EVENT_SUMMONING_HORROR, 20000);
				events.ScheduleEvent(EVENT_SUMMONING_GHULS, 30000);
			}

			void JustDied(Unit* killer)
			{
				BossAI::JustDied(killer);
				Talk(SAY_DEATH);
			};

			void EnterCombat(Unit* victim)
			{
				 if (!instance->CheckRequiredBosses(DATA_THE_LICH_KING, victim->ToPlayer()))
                {
                    EnterEvadeMode();
                    instance->DoCastSpellOnPlayers(LIGHT_S_HAMMER_TELEPORT);
                    return;
                }
					 BossAI::EnterCombat(victim);
					 DoCast(me, SPELL_SUMMONING_GHULS);
					 Talk(SAY_COMBAT);
			}

			void JustReachedHome()
            {
                BossAI::JustReachedHome();
                instance->SetBossState(DATA_THE_LICH_KING, FAIL);
			}

			void KilledUnit(Unit* victim)
			{
				if (victim->GetTypeId() == TYPEID_PLAYER)
                    Talk(SAY_KILL);
				}

			void UpdateAI(uint32 const diff)
			{
                if (!UpdateVictim() || !CheckInRoom())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STAT_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_BERSERK:
                            DoScriptText(EMOTE_GENERIC_BERSERK_RAID, me);
                            Talk(SAY_BERSERK);
                            DoCast(me, SPELL_BERSERK);
                            break;
						case EVENT_SUMMONING_HORROR:
							Talk(SAY_HORROR);
							DoCast(me, SPELL_SUMMONING_HORROR);
							break;
						case EVENT_SUMMONING_GHULS:
							Talk(SAY_GHULS);
							DoCast(SPELL_SUMMONING_GHULS);
							break;
						default:
							break;
					}
				}

				DoMeleeAttackIfReady();
			}

		};

		CreatureAI* GetAI(Creature* creature) const
        {
            return GetIcecrownCitadelAI<boss_the_lich_kingAI>(creature);
        }
};
void AddSC_boss_the_lich_king()
{
	new boss_the_lich_king();
};

